package com.soft.dao;

import com.soft.entity.Goods;
import com.soft.entity.UserInfo;
import com.soft.util.Connect;

import java.util.ArrayList;
import java.util.List;

public class UserInfoDao{

    Class<UserInfo> userInfoClass;

    public UserInfoDao() {
        userInfoClass = (Class<UserInfo>)UserInfo.class;
    }

    public UserInfo get(String account) {
        List<Object> list = new ArrayList<Object>();
        list.add(account);
        String sql = "select * from userinfo where id = ?";
        UserInfo userInfo = new UserInfo();
        if (Connect.getInstance().queryObject(sql,list,userInfoClass) != null)
            userInfo = Connect.getInstance().queryObject(sql,list,userInfoClass);
        return userInfo;
    }

    public List<UserInfo> getAll() {
        List<Object> param = new ArrayList<Object>();
        List<UserInfo> list = new ArrayList<UserInfo>();
        String sql = "select * from userinfo";
        list = Connect.getInstance().queryListObject(sql, param, userInfoClass);
        return list;
    }

    public boolean UserLogin(String account, String pwd) {
        List<Object> list = new ArrayList<Object>();
        list.add(account);
        list.add(pwd);
        String sql = "select * from userinfo where id = ? and password = ?";
        UserInfo userInfo = Connect.getInstance().queryObject(sql,list,userInfoClass);
        if(userInfo != null)
            return true;
        return false;
    }

    public boolean Update(UserInfo ui) {
        List<Object> list = new ArrayList<Object>();
        list.add(ui.getName());
        list.add(ui.getLevel());
        list.add(ui.getPassword());
        list.add(ui.getAddress());
        list.add(ui.getPostalCode());
        list.add(ui.getIdNumber());
        list.add(ui.getIdType());
        list.add(ui.getTelephone());
        list.add(ui.getEmail());
        list.add(ui.getStatus());
        list.add(ui.getId());

        String sql = "update userinfo set name=?, level=?, password=?, address=?, " +
                "postalCode=?, idNumber=?, idType=?, telephone=?, email=?, status=? where id=?";
        int flag = Connect.getInstance().execute(sql, list);
        if (flag != 0) {
            return true;
        }
        return  false;
    }

    public boolean Register(UserInfo ui) {
        List<Object> list = new ArrayList<Object>();
        list.add(ui.getId());
        list.add(ui.getName());
        list.add(ui.getLevel());
        list.add(ui.getPassword());
        list.add(ui.getAddress());
        list.add(ui.getPostalCode());
        list.add(ui.getIdNumber());
        list.add(ui.getIdType());
        list.add(ui.getTelephone());
        list.add(ui.getEmail());
        list.add(ui.getStatus());


        String sql = "insert into userinfo values (?,?,?,?,?,?,?,?,?,?,?) " ;

        int flag = Connect.getInstance().execute(sql, list);
        if (flag != 0) {
            return true;
        }
        return  false;
    }

}
